package protocol

import (
	"encoding/json"
	"errors"
	"fmt"
	"iot-base/common/cache"
	"iot-base/devc/common/jsonrpc"
)

func init() {

}

func DoRTSPPush(tid string, params map[string]interface{}) (interface{}, error) {
	pThing, err := cache.GetThing(tid)
	if pThing == nil || err != nil {
		return nil, err
	}
	//src := params["srcurl"]

	rpc := &jsonrpc.JSONRPCRequest{
		JSONRPC: "2.0",
		Method:  "doRTSPPush",
		Params:  params,
	}
	ip := pThing.GetThingParam("ip", "127.0.0.1")
	pport := pThing.GetThingParam("httpPort", "10080")
	pwd := pThing.GetThingParam("password", "admin")
	un := pThing.GetThingParam("username", "admin") //thing.Params[""]
	resp, errs := sendHTTP("POST", rpc, "http://"+ip+":"+pport+"/ISFIT/Media/RTSP/beginPush", un, pwd)
	fmt.Println("send cmd to media-------", string(resp), errs)
	if errs != nil {
		return nil, errs
	}
	var res map[string]interface{}
	errj := json.Unmarshal(resp, &res)
	if errj != nil {
		return nil, errj
	}
	ok := res["result"]
	if ok != nil {
		rok := ok.(string)
		if rok == "success" {
			fmt.Println("sucess------------", ip, pport, pwd, un)
			return nil, nil
		}
	} else {
		err := res["error"]
		if err != nil {
			emap := err.(map[string]interface{})
			if emap != nil {
				msg := emap["message"].(string)
				fmt.Println("found ----cmd return err--------msg", msg)
				return nil, errors.New(msg)
			}
			fmt.Println("found ----cmd return err--------token", err)
			return nil, errors.New("return error")
		}
	}
	fmt.Println("found ----cmd return err-----2222---token")
	return nil, errors.New("error from mm")

}
